package com.hw.paltfrom.service.system;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hw.paltfrom.mapper.HwBaseOffsiteMapper;
import com.hw.paltfrom.pojo.HwBaseOffsite;
import com.hw.paltfrom.rescode.HWSystemCode;
import com.hw.paltfrom.utils.LanguageUtil;
import com.hw.paltfrom.utils.RandomNumberUtil;
import com.hw.paltfrom.utils.ResponeResultUtil;
import com.hw.paltfrom.vo.BaseOffSiteVo;

@Service
public class BaseOffSiteServer {

	@Autowired
	private HwBaseOffsiteMapper baseOffsiteMapper;

	public Map<String, Object> insert(BaseOffSiteVo vo) {

		// 查看是否名称重复
		Wrapper<HwBaseOffsite> namewrapper = new EntityWrapper<HwBaseOffsite>();
		namewrapper.eq("F_OffSiteName", vo.getfOffsitename()).and().eq("F_DeleteMark", "0");
		List<HwBaseOffsite> selectList = baseOffsiteMapper.selectList(namewrapper);
		if (!selectList.isEmpty()) {
			return ResponeResultUtil.fail(HWSystemCode.OFFSITENAME_EXIT);
		}

//		// 查看是否编码重复
//		Wrapper<HwBaseOffsite> codewrapper = new EntityWrapper<HwBaseOffsite>();
//		codewrapper.eq("F_OffSiteCode", vo.getfOffsitecode()).and().eq("F_DeleteMark", "0");
//		List<HwBaseOffsite> codeList = baseOffsiteMapper.selectList(codewrapper);
//		if (!codeList.isEmpty()) {
//			return ResponeResultUtil.fail(HWSystemCode.OFFSITECODE_EXIT);
//		}

		// 插入数据库

		HwBaseOffsite baseOffsite = new HwBaseOffsite();
		baseOffsite.setfOffsiteid(RandomNumberUtil.getNumber());
		baseOffsite.setfAreaid(vo.getfAreaid());
		baseOffsite.setfOffsitecode(RandomNumberUtil.getCode());
		baseOffsite.setfOffsitename(vo.getfOffsitename());
		baseOffsite.setfQuickquery(LanguageUtil.toHanyuPinyin(vo.getfOffsitename()));
		baseOffsite.setfSimplespelling(vo.getfSimplespelling());
		baseOffsite.setfLongitude(vo.getfLongitude());
		baseOffsite.setfLatitude(vo.getfLatitude());
		baseOffsite.setfAddress(vo.getfAddress());
		baseOffsite.setfManager(vo.getfManager());
		baseOffsite.setfTel(vo.getfTel());
		baseOffsite.setfPic(vo.getfPic());
		baseOffsite.setfSortcode(vo.getfSortcode());
		baseOffsite.setfDeletemark(0);
		baseOffsite.setfEnabledmark(1);
		baseOffsite.setfDescription(vo.getfDescription());
		baseOffsite.setfCreatedate(new Timestamp(System.currentTimeMillis()));
		baseOffsite.setfCreateuserid(vo.getfCreateuserid());
		baseOffsite.setfCreateusername(vo.getfCreateusername());
		
//		2019-11-21新增字段
		baseOffsite.setfVideourl(vo.getfVideourl());
		baseOffsite.setfVideouser(vo.getfVideouser());
		baseOffsite.setfVideopassword(vo.getfVideopassword());
		baseOffsite.setfVediochannel(vo.getfVediochannel());
		baseOffsite.setfVediochannelname(vo.getfVediochannelname());
		Integer insert = baseOffsiteMapper.insert(baseOffsite);
		if (insert != 1) {
			return ResponeResultUtil.fail(HWSystemCode.FAIL);
		}
		return ResponeResultUtil.success(HWSystemCode.SUCCESS);
	}

	public Map<String, Object> queryAllList(BaseOffSiteVo vo) {

		// 模糊查询规则
		Wrapper<HwBaseOffsite> namewrapper = new EntityWrapper<HwBaseOffsite>();
		
		if(StringUtils.isNotBlank(vo.getfAreaid())){
			namewrapper.eq("F_AreaId", vo.getfAreaid());
		}
		
		if (StringUtils.isNotBlank(vo.getfOffsitecode())) {
			namewrapper.like("F_OffSiteCode", vo.getfOffsitecode());
		} 
		
		if(StringUtils.isNotBlank(vo.getfOffsitename())){
			namewrapper.like("F_OffSiteName", vo.getfOffsitename());
		}
		namewrapper.eq("F_DeleteMark", "0").orderBy("F_SortCode");
//		if (StringUtils.isNotBlank(vo.getfOffsitecode())) {
//			namewrapper.like("F_OffSiteCode", vo.getfOffsitecode()).and().eq("F_DeleteMark", "0").orderBy("F_SortCode");
//		} else if (StringUtils.isNotBlank(vo.getfOffsitename())) {
//			namewrapper.like("F_OffSiteName", vo.getfOffsitename()).and().eq("F_DeleteMark", "0").orderBy("F_SortCode");
//		} else if (StringUtils.isBlank(vo.getfOffsitename()) && StringUtils.isBlank(vo.getfOffsitecode())) {
//			namewrapper.eq("F_DeleteMark", "0").orderBy("F_SortCode");
//		}
		Integer pageNum = vo.getPageNum();
		Integer pageSize = vo.getPageSize();
		PageHelper.startPage(pageNum, pageSize);
		
		List<HwBaseOffsite> hwBaseAreasList = baseOffsiteMapper.selectList(namewrapper);
		PageInfo<HwBaseOffsite> info = new PageInfo<HwBaseOffsite>(hwBaseAreasList);
		return ResponeResultUtil.successNotice(HWSystemCode.SUCCESS, info);
	}
	
	public Map<String, Object> update(BaseOffSiteVo vo) {
		
		// 通过ID查询待更新的数据
		Wrapper<HwBaseOffsite> wrapper = new EntityWrapper<HwBaseOffsite>();
		wrapper.eq("F_OffSiteID", vo.getfOffsiteid());
		
		HwBaseOffsite baseOffsite = new HwBaseOffsite();
//		如果删除标记为1的话，当前是删除操作
		if(vo.getfDeletemark()==1){
			baseOffsite.setfDeletemark(vo.getfDeletemark());
			baseOffsite.setfModifydate(new Timestamp(System.currentTimeMillis()));
			baseOffsite.setfModifyuserid(vo.getfModifyuserid());
			baseOffsite.setfModifyusername(vo.getfModifyusername());
			Integer update = baseOffsiteMapper.update(baseOffsite, wrapper);
			if (update != 1) {
				return ResponeResultUtil.fail(HWSystemCode.FAIL);
			}
		}else{
			
			// 查看是否名称重复
			Wrapper<HwBaseOffsite> namewrapper = new EntityWrapper<HwBaseOffsite>();
			namewrapper.eq("F_OffSiteName", vo.getfOffsitename()).and().ne("F_OffSiteID", vo.getfOffsiteid());
			List<HwBaseOffsite> selectList = baseOffsiteMapper.selectList(namewrapper);
			if (!selectList.isEmpty()) {
				return ResponeResultUtil.fail(HWSystemCode.OFFSITENAME_EXIT);
			}
			
			baseOffsite.setfAreaid(vo.getfAreaid());
			baseOffsite.setfOffsitename(vo.getfOffsitename());
			baseOffsite.setfQuickquery(LanguageUtil.toHanyuPinyin(vo.getfOffsitename()));
			baseOffsite.setfSimplespelling(vo.getfSimplespelling());
			baseOffsite.setfLongitude(vo.getfLongitude());
			baseOffsite.setfLatitude(vo.getfLatitude());
			baseOffsite.setfAddress(vo.getfAddress());
			baseOffsite.setfManager(vo.getfManager());
			baseOffsite.setfTel(vo.getfTel());
			baseOffsite.setfPic(vo.getfPic());
			baseOffsite.setfSortcode(vo.getfSortcode());
			baseOffsite.setfDeletemark(vo.getfDeletemark());
			baseOffsite.setfEnabledmark(vo.getfEnabledmark());
			baseOffsite.setfDescription(vo.getfDescription());
			baseOffsite.setfModifydate(new Timestamp(System.currentTimeMillis()));
			baseOffsite.setfModifyuserid(vo.getfModifyuserid());
			baseOffsite.setfModifyusername(vo.getfModifyusername());
			
//			2019-11-21新增字段
			baseOffsite.setfVideourl(vo.getfVideourl());
			baseOffsite.setfVideouser(vo.getfVideouser());
			baseOffsite.setfVideopassword(vo.getfVideopassword());
			baseOffsite.setfVediochannel(vo.getfVediochannel());
			baseOffsite.setfVediochannelname(vo.getfVediochannelname());
			
			Integer update = baseOffsiteMapper.update(baseOffsite, wrapper);
			if(update!=1){
				return ResponeResultUtil.fail(HWSystemCode.FAIL);
			}
			
		}
		return ResponeResultUtil.success(HWSystemCode.SUCCESS);
	}


}
